const getAccessToken = require('./getAccessToken.js')
const rp = require('request-promise')

const callCloudDb = async(ctx,fnName,query={})=>{
    const ACCESS_TOKEN = await getAccessToken()
    const options = {
        method: 'POST',
        uri: `https://api.weixin.qq.com/tcb/${fnName}?access_token=${ACCESS_TOKEN}&env=${ctx.state.env}&name=${fnName}`,
        body: {
            query,
            env:ctx.state.env
        },
        json: true // Automatically stringifies the body to JSON
    }

    return await rp(options)
        .then(res=>{
            return res
        })
        .catch(err=>{
            console.log(err)
        })
}

module.exports = callCloudDb